//
//  TabBarController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/21.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class TabBarController: UITabBarController {
    
    lazy var items : [UITabBarItem] = [UITabBarItem]()
    
    lazy var myTabBar : ZMTabBar = ZMTabBar()

    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        addChildViewControllers()
        
        setupAllTabBarButton()

        
        setupTabBar()
        // Do any additional setup after loading the view.
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        for item in tabBar.items!{
        
        item.isEnabled = false
        
        }

        

        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

  
}








extension  TabBarController{

    fileprivate  func addChildViewControllers() {
        
        let temp = UserDefaults.standard.value(forKey: "IsNewCityUser")
        
        
        if temp != nil {
            
            let str = temp! as! String
            
            if str == "TRUE" {
                
                addChildViewController(childController: NewCityHomeVC(),title: "首页")
                addChildViewController(childController: NewCityGoodsClassify(),title: "分类")
                
            } else {
                
                addChildViewController(childController: HomeViewController(),title: "首页")
                addChildViewController(childController: GoodsClassifyController(),title: "分类")
                
            }
            
            
            
        } else {
            
            addChildViewController(childController: HomeViewController(),title: "首页")
            addChildViewController(childController: GoodsClassifyController(),title: "分类")
            
        }

        
        
        
        
        
        
        
        if let hehe = temp as? String {
            
            if hehe == "TRUE" {
                
                addChildViewController(childController: DiscoverController(),title: "发现")
                
            }
        }
        
        addChildViewController(childController: ShoppingCarController(),title: "购物车")
        
        addChildViewController(childController: PersonalController(),title: "我的")
    }
    
    
    private  func addChildViewController(childController: UIViewController ,title : String) {
        
        childController.navigationItem.title = title

        let navi = BaseNavController(rootViewController: childController)
        
        addChildViewController(navi)
    }
    
    
    fileprivate func setupAllTabBarButton(){
        
        let tabBarItem0 : UITabBarItem = UITabBarItem()
        tabBarItem0.title = "首页"
        tabBarItem0.image = UIImage(named: "tabBar_home_normal")
        tabBarItem0.selectedImage = UIImage.imageWithOriginal(imageName: "tabBar_home_press")
        items.append(tabBarItem0)
        
        let tabBarItem1 : UITabBarItem = UITabBarItem()
        tabBarItem1.title = "分类"
        tabBarItem1.image = UIImage(named: "tabBar_category_normal")
        tabBarItem1.selectedImage = UIImage.imageWithOriginal(imageName: "tabBar_category_press")
        items.append(tabBarItem1)
        
        
        let temp = UserDefaults.standard.value(forKey: "IsNewCityUser")
        
        if let hehe = temp as? String {
            
            if hehe == "TRUE" {
                
                let tabBarItemNew : UITabBarItem = UITabBarItem()
                tabBarItemNew.title = "发现"
                tabBarItemNew.image = UIImage(named: "tabBar_find_normal")
                tabBarItemNew.selectedImage = UIImage.imageWithOriginal(imageName: "tabBar_find_press")
                items.append(tabBarItemNew)
                
            }
        }
        
        
        let tabBarItem2 : UITabBarItem = UITabBarItem()
        tabBarItem2.title = "购物车"
        tabBarItem2.image = UIImage(named: "tabBar_cart_normal")
        tabBarItem2.selectedImage = UIImage.imageWithOriginal(imageName: "tabBar_cart_press")
        items.append(tabBarItem2)
        
        
        
        
        let tabBarItem3 : UITabBarItem = UITabBarItem()
        tabBarItem3.title = "我的"
        tabBarItem3.image = UIImage(named: "tabBar_myJD_normal")
        tabBarItem3.selectedImage = UIImage.imageWithOriginal(imageName: "tabBar_myJD_press")
        items.append(tabBarItem3)
    
    
    
    }
    
    
    fileprivate func setupTabBar(){
    
        tabBar.addSubview(myTabBar)
    
        myTabBar.frame = tabBar.bounds
        
        
        myTabBar.items = items

 
        
        myTabBar.delegate = self
        
        
        myTabBar.backgroundColor = UIColor.clear
        
     
    }

}



extension TabBarController : ZMTabBarProtool{


    func tabBarDidSelect(tabBar : ZMTabBar,index : Int){
    
    
      selectedIndex = index
    
    
    }


}
